<?php
 
 // no direct access
defined('_JEXEC') or die('Restricted access');

// import application controller
jimport('joomla.application.component.controller');

class SyncrControllerSites extends JController
{
	public function __construct($config = array()) {
		parent::__construct($config);
		
		// Register extra tasks
		$this->registerTask('apply',	 'save');
		$this->registerTask('add',		 'edit');
		$this->registerTask('unpublish', 'publish');
	}
	
	public function display()
	{
		// set view if not specified
		if (!$view = JRequest::getVar('view')) {
			JRequest::setVar('view', 'sites');
		}
		
		parent::display();
	}
	
	public function edit()
	{		
		$model = &$this->getModel('site');
		$model->checkout();
	
		$view = &$this->getView('site', 'html');
		$view->setModel($model, true);
		$view->display();
	}
	
	public function publish()
	{	
		if (!JRequest::checkToken('REQUEST')) {
			JError::raiseError(403, JText::_('ALERTNOAUTH'));
			jexit('Invalid token');
		}
	
		$cid	= JRequest::getVar('cid', array(0), 'post', 'array');
		$site =& JTable::getInstance('site', 'Table');
		
		// defaults
		$published = 1;
		if (count($cid) > 1) {
			$msg = JText::_('SYNCR_SELECTED_SITES_HAVE_BEEN_PUBLISHED');
		} else {
			$msg = JText::_('SYNCR_SELECTED_SITE_HAS_BEEN_PUBLISHED');
		}
		
		// if unpublishing
		if ($this->getTask() === 'unpublish') {
			$published = 0;
			if (count($cid) > 1) {
				$msg = JText::_('SYNCR_SELECTED_SITES_HAVE_BEEN_UNPUBLISHED');
			} else {
				$msg = JText::_('SYNCR_SELECTED_SITE_HAS_BEEN_UNPUBLISHED');
			}
		}
		
		if (!$site->publish($cid, $published)) {
			JError::raiseError(500, $site->getError());
		}
				
		$this->setRedirect('?option=com_syncr&controller=sites', $msg);
	}
	
	public function remove()
	{
		if (!JRequest::checkToken('REQUEST')) {
			JError::raiseError(403, JText::_('ALERTNOAUTH'));
			jexit('Invalid token');
		}
		
		$cid = JRequest::getVar('cid', array());
		$site =& JTable::getInstance('site', 'Table');
		
		foreach ($cid as $id)
		{
			$id = (int) $id;
			if (!$site->delete($id)) {
				JError::raiseError(500, $site->getError());
			}
		}
		
		if (count($cid) > 1) {
			$msg = JText::_('SYNCR_SELECTED_SITES_HAVE_BEEN_DELETED');
		} else {
			$msg = JText::_('SYNCR_SELECTED_SITE_HAS_BEEN_DELETED');
		}
		
		$this->setRedirect('?option=com_syncr&controller=sites', $msg);
	}
		
	public function save()
	{	
		if (!JRequest::checkToken('REQUEST')) {
			JError::raiseError(403, JText::_('ALERTNOAUTH'));
			jexit('Invalid token');
		}
		
		$post	= JRequest::get('post');
		$cid	= JRequest::getVar('cid', array(0), 'post', 'array');
		$post['id'] = (int) $cid[0];
		
		$model = $this->getModel('site');
		
		if (!$id = $model->store($post)) {
			JError::raiseError(500, $model->getError());
		}
		
		$model->checkin();
		
		// apply
		if ($this->getTask() == 'apply') {
			$msg = 'SYNCR_CHANGES_HAVE_BEEN_APPLIED';
			$this->setRedirect('?option=com_syncr&controller=sites&task=edit&cid[]='.$id, $msg);
		} else {
			$msg = 'SYNCR_CHANGES_HAVE_BEEN_SAVED';
			$this->setRedirect('?option=com_syncr&controller=sites', $msg);
		}
	}
	
	function cancel()
	{
		if (!JRequest::checkToken('REQUEST')) {
			JError::raiseError(403, JText::_('ALERTNOAUTH'));
			jexit('Invalid token');
		}

		$model = $this->getModel('site');
		$model->checkin();

		$this->setRedirect('index.php?option=com_syncr&controller=sites');
	}
}